package com.example.my_helper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;

public class DBHelper extends SQLiteOpenHelper {
    public SQLiteDatabase db;
    String sql;
    public boolean isOpen = false;

    public DBHelper(Context context) {
        super(context,"myhelp_db",null,1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        sql = "create table kuaijie_list(name varchar(255), content varchar(255))";
        db.execSQL(sql);

        sql = "create table register(" +
                    "id long PRIMARY KEY, " +
                    "name varchar(255), " +
                    "device_addr varchar(30), " +
                    "register_addr varchar(30), " +
                    "register_class int, " +
                    "data_type int, " +
                    "swapped int, " +
                    "hex_show int, " +
                    "ratio float, " +
                    "unit varchar(30), " +
                    "period int" +
                ")";
        db.execSQL(sql);

        //订阅主题
        sql = "create table subscribe_topic(topic varchar(255), qos int)";
        db.execSQL(sql);

        //mqtt设置
        sql = "create table mqtt_setting(" +
                    "id int, " +
                    "host varchar(30), " +
                    "port int, " +
                    "username varchar(255), " +
                    "password varchar(255)," +
                    "client_id varchar(255), " +
                    "keep_alive int, " +
                    "clean_seasion int, " +
                    "will_topic varchar(255), " +
                    "will_message varchar(1024), " +
                    "will_qos int, " +
                    "will_retained int," +
                    "qos int" +
                ")";
        db.execSQL(sql);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void setting_updata()
    {

    }

    public void kuaijie_delete(String content)
    {
        if(!isOpen)
            return;
        db.delete("kuaijie_list","content=?",new String[]{content});
    }

    public void kuaijie_insert(String name, String content)
    {
        if (!isOpen){
            return;
        }
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("content", content);
        db.insert("kuaijie_list", null, values);
    }

    public ArrayList<KuaijieText> kuaijie_getdata()
    {
        if (!isOpen)
            return null;
        ArrayList<KuaijieText> list = new ArrayList<KuaijieText>();
        Cursor cursor = db.query("kuaijie_list", null, null, null, null, null, null);
        while(cursor.moveToNext()){
            KuaijieText text = new KuaijieText(cursor.getString(0), cursor.getString(1));
            list.add(0, text);
        }
        return list;
    }

    public void register_delete(long id)
    {
        if(!isOpen)
            return;
        db.delete("register","id=?",new String[]{String.valueOf(id)});
    }

    public void register_insert(Register reg)
    {
        if (!isOpen){
            return;
        }
        ContentValues values = new ContentValues();
        values.put("id", reg.id1);
        values.put("name", reg.register_name);
        values.put("device_addr", reg.device_addr_str);
        values.put("register_addr", reg.register_addr_str);
        values.put("register_class", reg.register_class);
        values.put("data_type", reg.data_type);
        values.put("swapped", reg.swapped);
        values.put("hex_show", reg.hex_show);
        values.put("ratio", reg.ratio);
        values.put("unit", reg.unit);
        values.put("period", reg.period);
        db.insert("register", null, values);
    }

    public void register_update(Register reg){
        if (!isOpen){
            return;
        }
        ContentValues values = new ContentValues();
        values.put("id", reg.id1);
        values.put("name", reg.register_name);
        values.put("device_addr", reg.device_addr_str);
        values.put("register_addr", reg.register_addr_str);
        values.put("register_class", reg.register_class);
        values.put("data_type", reg.data_type);
        values.put("swapped", reg.swapped);
        values.put("hex_show", reg.hex_show);
        values.put("ratio", reg.ratio);
        values.put("unit", reg.unit);
        values.put("period", reg.period);
        db.update("register", values, "id=?", new String[]{String.valueOf(reg.id1)});
    }

    public ArrayList<Register> register_getdata()
    {
        if (!isOpen)
            return null;
        ArrayList<Register> list = new ArrayList<Register>();
        Cursor cursor = db.query("register", null, null, null, null, null, null);
        while(cursor.moveToNext()){
            Register reg = new Register();
            reg.id1 = cursor.getLong(0);
            reg.register_name = cursor.getString(1);
            reg.setDevice_addr(cursor.getString(2));
            reg.setRegister_addr(cursor.getString(3));
            reg.setRegister_type(cursor.getInt(4));
            reg.data_type = cursor.getInt(5);
            reg.swapped = cursor.getInt(6)==1;
            reg.hex_show = cursor.getInt(7)==1;
            reg.ratio = cursor.getFloat(8);
            reg.unit = cursor.getString(9);
            reg.period = cursor.getInt(10);
            list.add(0, reg);
        }
        return list;
    }

    public void subscribe_topic_delete(String topic)
    {
        if(!isOpen)
            return;
        db.delete("subscribe_topic","topic=?",new String[]{topic});
    }

    public void subscribe_topic_insert(String topic, int qos)
    {
        if (!isOpen){
            return;
        }
        ContentValues values = new ContentValues();
        values.put("topic", topic);
        values.put("qos", qos);
        db.insert("subscribe_topic", null, values);
    }

    public ArrayList<SubscribeTopic> subscribe_topic_getdata()
    {
        if (!isOpen)
            return null;
        ArrayList<SubscribeTopic> list = new ArrayList<>();
        int i = 0;
        Cursor cursor = db.query("subscribe_topic", null, null, null, null, null, null);
        while(cursor.moveToNext()){
            SubscribeTopic s = new SubscribeTopic();
            s.topic = cursor.getString(0);
            s.qos = cursor.getInt(0);
            list.add(s);
        }
        return list;
    }

    public void mqtt_setting_insert(MyMQTTConnection conn)
    {
        if (!isOpen){
            return;
        }
        ContentValues values = new ContentValues();
        values.put("id", conn.connecttion_id);
        values.put("host", conn.host);
        values.put("port", conn.port);
        values.put("username", conn.username);
        values.put("password", conn.password);
        values.put("client_id", conn.client_id);
        values.put("keep_alive", conn.keep_alive);
        values.put("clean_seasion", conn.clean_seasion);
        values.put("will_topic", conn.last_will_topic);
        values.put("will_message", conn.last_will_message);
        values.put("will_qos", conn.last_will_qos);
        values.put("will_retained", conn.last_will_retained);
        db.insert("mqtt_setting", null, values);
    }

    public void mqtt_setting_update(MyMQTTConnection conn){
        if (!isOpen){
            return;
        }
        ContentValues values = new ContentValues();
        values.put("host", conn.host);
        values.put("port", conn.port);
        values.put("username", conn.username);
        values.put("password", conn.password);
        values.put("client_id", conn.client_id);
        values.put("keep_alive", conn.keep_alive);
        values.put("clean_seasion", conn.clean_seasion);
        values.put("will_topic", conn.last_will_topic);
        values.put("will_message", conn.last_will_message);
        values.put("will_qos", conn.last_will_qos);
        values.put("will_retained", conn.last_will_retained);
        db.update("mqtt_setting", values, "id=?", new String[]{String.valueOf(conn.connecttion_id)});
    }

    public MyMQTTConnection mqtt_setting_getdata()
    {
        if (!isOpen)
            return null;
        MyMQTTConnection conn = new MyMQTTConnection("8.129.170.160", 1883, "username", "password", null);
        Cursor cursor = db.query("mqtt_setting", null, null, null, null, null, null);
        if(cursor.getCount() > 0){
            cursor.moveToNext();
            conn.connecttion_id = cursor.getInt(0);
            conn.host = cursor.getString(1);
            conn.port= cursor.getInt(2);
            conn.username = cursor.getString(3);
            conn.password = cursor.getString(4);
            //conn.client_id = cursor.getString(5);
            conn.keep_alive = cursor.getInt(6);
            conn.clean_seasion = cursor.getInt(7)==1;
            conn.last_will_topic = cursor.getString(8);
            conn.last_will_message = cursor.getString(9);
            conn.last_will_qos = cursor.getInt(10);
            conn.last_will_retained = cursor.getInt(11)==1;
        }else{
            mqtt_setting_insert(conn);
        }
        return conn;
    }

    public void open()
    {
        db = this.getWritableDatabase();
        isOpen = true;

//        sql = "drop table kuaijie_list";
//        db.execSQL(sql);
//
//        sql = "create table kuaijie_list(name varchar(255), content varchar(255))";
//        db.execSQL(sql);

//        //订阅主题
//        sql = "create table subscribe_topic(topic varchar(255), qos int)";
//        db.execSQL(sql);
//
        //删除表
        sql = "drop table mqtt_setting";
        db.execSQL(sql);

        //mqtt设置
        sql = "create table mqtt_setting(" +
                "id int PRIMARY KEY, " +
                "host varchar(30), " +
                "port int, " +
                "username varchar(255), " +
                "password varchar(255)," +
                "client_id varchar(255), " +
                "keep_alive int, " +
                "clean_seasion int, " +
                "will_topic varchar(255), " +
                "will_message varchar(1024), " +
                "will_qos int, " +
                "will_retained int," +
                "qos int" +
                ")";
        db.execSQL(sql);
        //mqtt_setting_insert(new MyMQTTConnection("8.129.170.160",  1883, "uasername", "password", null));
    }

    public void close()
    {
        db.close();
        isOpen = false;
    }
}
